package com.example.schooltest;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.schooltest.adapter.VPFragmentAdapter;
import com.example.schooltest.entity.UserIm;
import com.example.schooltest.fragment.CallPhoneFragment;
import com.example.schooltest.fragment.FineFragment;
import com.example.schooltest.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.BoxStore;

public class LoginMainActivity extends AppCompatActivity implements View.OnClickListener {

    private VPFragmentAdapter vpFragmentAdapter;
    private LinearLayout llHome,llFine, llMine;
    private TextView tvHome, tvFine, tvMine;
    private ImageView ivHome,ivFine, ivMine;



    private ViewPager viewPager;
    private VPFragmentAdapter fragmentAdapter;
    List<Fragment> list ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login_main);

        initView();
        initDate();
        fragmentAdapter = new VPFragmentAdapter(getSupportFragmentManager(), list);
        viewPager.setAdapter(fragmentAdapter);
        onViewPagerSelect(2);
        viewPager.setCurrentItem(2);
        llHome.setOnClickListener(this);
        llFine.setOnClickListener(this);
        llMine.setOnClickListener(this);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                Toast.makeText(LoginMainActivity.this, "页面" + position, Toast.LENGTH_SHORT).show();
                onViewPagerSelect(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });




    }

    private void onViewPagerSelect(int position) {
        ivHome.setSelected(false);
        ivFine.setSelected(false);
        ivMine.setSelected(false);
        tvMine.setTextColor(getResources().getColor(R.color.myGray));
        tvFine.setTextColor(getResources().getColor(R.color.myGray));
        tvHome.setTextColor(getResources().getColor(R.color.myGray));
        switch (position) {
            case 0:
                ivHome.setSelected(true);
                tvHome.setTextColor(getResources().getColor(R.color.green));
                break;
            case 1:
                ivFine.setSelected(true);
                tvFine.setTextColor(getResources().getColor(R.color.green));
                break;
            case 2:
                ivMine.setSelected(true);
                tvMine.setTextColor(getResources().getColor(R.color.green));
                break;
        }
    }

    private void initDate() {
        list = new ArrayList<>();
        FineFragment fineFragment = FineFragment.newInstance("", "");
        HomeFragment homeFragment = HomeFragment.newInstance("", "");
        CallPhoneFragment callPhoneFragment = CallPhoneFragment.newInstance("", "");

        list.add(homeFragment);
        list.add(fineFragment);
        list.add(callPhoneFragment);

    }

    private void initView() {

        viewPager = (ViewPager) findViewById(R.id.vp);
        llHome = findViewById(R.id.ll_home);
        llFine = findViewById(R.id.ll_find);
        llMine = findViewById(R.id.ll_mine);

        tvHome = findViewById(R.id.tv_home);
        tvFine = findViewById(R.id.tv_find);
        tvMine = findViewById(R.id.tv_mine);

        ivHome = findViewById(R.id.iv_home);
        ivFine = findViewById(R.id.iv_find);
        ivMine = findViewById(R.id.iv_mine);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_home:
                viewPager.setCurrentItem(0);
                break;
            case R.id.ll_find:
                viewPager.setCurrentItem(1);
                break;
            case R.id.ll_mine:
                viewPager.setCurrentItem(2);
                break;
        }
    }
}